<?php
/**
 * SvideoService
 */
namespace App\Service;

use Phalcon\Di;
use App\Models\Video;
use App\Models\EwVideo;

class SvideoService  extends BaseService
{
	const MIGRATE_TRUE = 1;		//入库标识：是
	const MIGRATE_FALSE = 0;	//入库标识：否
	const DEL_TRUE = 1;			//删除标识：是
	const DEL_FALSE = 0;		//删除标识：否

	/**
	 * [getList 列表 列表]
	 * @param  [type] $params [description]
	 * @return [type]         [description]
	 */
	public static function getList($params, $count = FALSE)
	{
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns(['v.id,v.title,v.outline,v.thumb,v.s,v.tags,v.publish_time,v.source,v.video_url,v.url']);
		$builder->addFrom("App\Models\Video", 'v');
		$builder->andWhere("v.is_del = " . self::DEL_FALSE);

		//is_migrate 
		if (isset($params->is_migrate))
			$builder->andWhere("v.is_migrate = ". $params->is_migrate);

		//title
		if (isset($params->title) && $params->title)
			$builder->andwhere("v.title LIKE '%" . $params->title . "%'");

		//outline
		if (isset($params->outline) && $params->outline)
			$builder->andwhere("v.outline LIKE '%" . $params->outline . "%'");

		//tags
		if (isset($params->tags) && $params->tags)
			$builder->andwhere("v.tags LIKE '%" . $params->tags . "%'");

		//source
		if (isset($params->source) && $params->source)
			$builder->andwhere("v.source LIKE '%" . $params->source . "%'");

		//sources
		if (isset($params->sources) && $params->sources)
			$builder->andwhere("v.source LIKE '%" . $params->sources . "%'");

		//count 数量
		if ($count)
			return $builder->getQuery()->execute()->count();

		//order_by 排序
		if (isset($params->order_by)){
			$sql = parent::orderBy($params->order_by, $table = "v");
		}else{
			$sql = " v.id DESC";
		}
		$builder->orderBy($sql);

		$builder->limit($params->page_num,($params->p-1)*$params->page_num);
		$result = $builder->getQuery();
		return $result->execute();	
	}

	/**
	 * [getInfo 详情]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function getInfo($id)
	{
		return Video::findFirst($id);
	}

	/**
	 * [delete 移除/恢复数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function delete($id)
	{
		if (isset($id) && $id)
			$data = Video::findFirst($id);

		if ($data){
			$data->is_del = $data->is_del ? self::DEL_FALSE : self::DEL_TRUE;
			return $data->save();
		}

		return NULL;
	}

	/**
	 * [forever 彻底删除数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		if (isset($id) && $id)
			$row = Video::findFirst($id);

		if ($row && $row->delete())
			return TRUE;

		return NULL;
	}

	/**
	 * [migrate 入库]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function migrate($id)
	{

		$info = Video::findFirst($id);

		if (!$info || $info->is_migrate == self::MIGRATE_TRUE )
			return NULL;
		//过滤 \n
		foreach ($info as $key => $value) {
			if ($key == "tags"){
				$short_title = preg_replace("/\\n/", " ", $info->tags);
				$type = explode(" ", $short_title);//类型取第一条
				$type = current($type) ? $type[0] : "觅窝";
			}
			$info->$key = preg_replace("/\\n/", "", $value);
			$info->$key = trim($info->$key);
		}

		$data = new \stdClass();
		$data->id = $id;
		$data->command = "SVIDEO";
		$data->city_id = 63;
		$data->title = $info->title;

		//2017-3-29 +
		$data->three_url = $info->url;
		$data->is_self = 0;

		// $data->tags = "";
		$data->short_title = $short_title;
		$data->summary = $info->outline;
		$data->cate_id = 12;
		$data->thumb = $info->thumb;
		$data->video = $info->video_url;
		$data->type = $type;
		$data->duration = $info->s;
		$data->sources = $info->source;
		// $data->download = 0;
		// $data->share = 0;
		// $data->visit = 0;
		// $data->comment = 0;
		// $data->is_display = 1;
		// $data->is_del = 0;
		// $data->order_by = 100;
		// $data->create_time = date("Y-m-d H:i:s", time());
		// $data->update_time = date("Y-m-d H:i:s", time());

		$redis = Di::getDefault()['redis'];
		$queue = Di::getDefault()['queue'];

		$redis->set("SVIDEO_{$info->id}", $info->id);
		$redis->expireAt("SVIDEO_{$info->id}", time() + 60*5);
		$queue->put(json_encode((array)$data));

		// $row = new EwVideo;

		// if (!$row->save((array)$data))
		// {
		// 	$msg =$row->getMessages();
		// 	var_dump($msg);die;
		// }
		// if ($row->save((array)$data))
		// {
		// 	$info->is_migrate = self::MIGRATE_TRUE;
		// 	$info->save();
		// }
	}

	/**
	 * [batchMigrate 批量入库]
	 * @param  [type] $id_arr [description]
	 * @return [type]         [description]
	 */
	public static function batchMigrate($id_arr = [])
	{
		if ($id_arr && is_array($id_arr) && count($id_arr) > 0){
			foreach ($id_arr as $id) {
				self::migrate($id);
			}
		}
	}
}